package com.liwei.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liwei.blog.dao.CatalogDao;
import com.liwei.blog.pojo.Catalog;
import com.liwei.blog.pojo.User;
import com.liwei.blog.service.CatalogService;

/*
* 2018年12月6日
*  liwei
*  	博客分类
*/
@Service
public class CatalogServiceImpl implements CatalogService{

	@Autowired
	private CatalogDao catalogDao;
	
	@Override
	public Catalog saveCatalog(Catalog catalog) {
		// 判断重复
		List<Catalog> list = catalogDao.findByUserAndName(catalog.getUser(), catalog.getName());
		if(list !=null && list.size() > 0) {
			throw new IllegalArgumentException("该分类已经存在了");
		}
		return catalogDao.save(catalog);
	}

	@Override
	public void removeCatalog(Long id) {
		catalogDao.deleteById(id);
	}

	@Override
	public Catalog getCatalogById(Long id) {
		return catalogDao.getOne(id);
	}

	@Override
	public List<Catalog> listCatalogsByUser(User user) {
		return catalogDao.findByUser(user);
	}

}
